﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using Module.EntitiesInfo;

namespace Module.DataAccess
{
	public class CMSGameDB
	{
		#region ConnectionString
        private string _connectionString;
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }
        public CMSGameDB()
        {
            ConnectionString = ConfigurationManager.ConnectionStrings["localsql"].ToString();
        }
        #endregion

		#region Methods

		/// <summary>
		/// Saves a record to the CMS_Game table.
		/// </summary>
		public virtual int Insert(CMSGameInfo cMSGameInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@NameGame", cMSGameInfo.NameGame),
				new SqlParameter("@CateId", cMSGameInfo.CateId),
				new SqlParameter("@Production", cMSGameInfo.Production),
				new SqlParameter("@LinkGame", cMSGameInfo.LinkGame),
				new SqlParameter("@Avatar", cMSGameInfo.Avatar),
				new SqlParameter("@CreatedOn", cMSGameInfo.CreatedOn),
				new SqlParameter("@CreatedBy", cMSGameInfo.CreatedBy),
				new SqlParameter("@Status", cMSGameInfo.Status),
				new SqlParameter("@ShowType", cMSGameInfo.ShowType),
				new SqlParameter("@Description", cMSGameInfo.Description),
				new SqlParameter("@Contents", cMSGameInfo.Contents)
			};

			return (int) SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "CMS_Game_Insert", parameters);
		}
		
		public virtual void Update(CMSGameInfo cMSGameInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdGame", cMSGameInfo.IdGame),
				new SqlParameter("@NameGame", cMSGameInfo.NameGame),
				new SqlParameter("@CateId", cMSGameInfo.CateId),
				new SqlParameter("@Production", cMSGameInfo.Production),
				new SqlParameter("@LinkGame", cMSGameInfo.LinkGame),
				new SqlParameter("@Avatar", cMSGameInfo.Avatar),
				new SqlParameter("@CreatedOn", cMSGameInfo.CreatedOn),
				new SqlParameter("@CreatedBy", cMSGameInfo.CreatedBy),
				new SqlParameter("@Status", cMSGameInfo.Status),
				new SqlParameter("@ShowType", cMSGameInfo.ShowType),
				new SqlParameter("@Description", cMSGameInfo.Description),
				new SqlParameter("@Contents", cMSGameInfo.Contents)
			};

			SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "CMS_Game_Update", parameters);
		}
		
		public virtual void Delete(int idGame)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdGame", idGame)
			};

			SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "CMS_Game_Delete", parameters);
		}

        public IDataReader Select(int? type, string para_1, string para_2)
        {
            try
            {
                return SqlHelper.ExecuteReader(ConnectionString, "CMS_Game_Select", type, para_1, para_2);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public IDataReader Select(int? type, string para_1)
        {
            try
            {
                return SqlHelper.ExecuteReader(ConnectionString, "CMS_Game_Select", type, para_1);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public IDataReader Select(int? type)
        {
            try
            {
                return SqlHelper.ExecuteReader(ConnectionString, "CMS_Game_Select", type);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable SelectTbl(int? type, string para_1, string para_2)
        {
            try
            {
                return SqlHelper.ExecuteDataset(ConnectionString, "CMS_Game_Select", type, para_1, para_2).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable SelectTbl(int? type, string para_1)
        {
            try
            {
                return SqlHelper.ExecuteDataset(ConnectionString, "CMS_Game_Select", type, para_1).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable SelectTbl(int? type)
        {
            try
            {
                return SqlHelper.ExecuteDataset(ConnectionString, "CMS_Game_Select", type).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }
        }

        public virtual int GetCount(string keysearch, int cateId, int status, DateTime starDatetime, DateTime endDatetime, int beginRow, int endRow)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{                          
               new SqlParameter("@keysearch", keysearch),  
               new SqlParameter("@CateId",cateId), 
               new SqlParameter("@Status",status),   
               new SqlParameter("@starDatetime", starDatetime),
               new SqlParameter("@endDatetime", endDatetime),
               new SqlParameter("@IsTotalRow", 1)
			};

            return (int)SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "CMS_Game_Search", parameters);
        }

        /// <summary>
        /// Tìm kiếm
        /// </summary>
        /// <param name="keysearch">Từ khóa tìm kiếm</param>
        /// <param name="starDatetime">Ngày bắt đầu</param>
        /// <param name="endDatetime">Ngày kết thúc</param>
        /// <param name="beginRow">Bản ghi đầu</param>
        /// <param name="endRow">Bản ghi cuối</param>
        /// <param name="orderByColumn">Sắp xếp</param>
        /// <returns></returns>
        public virtual DataTable Search(string keysearch, int cateId, int status, DateTime starDatetime, DateTime endDatetime, int beginRow, int endRow, string orderByColumn)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{                
                new SqlParameter("@Keysearch", keysearch),  
                new SqlParameter("@CateId",cateId), 
                new SqlParameter("@Status",status), 
                new SqlParameter("@StarDatetime", starDatetime),
                new SqlParameter("@EndDatetime", endDatetime),               
                new SqlParameter("@BeginRow", beginRow),
                new SqlParameter("@EndRow", endRow),
                new SqlParameter("@OrderByColumn", orderByColumn),
				new SqlParameter("@IsTotalRow", 0)
			};

            return SqlHelper.ExecuteDataset(ConnectionString, CommandType.StoredProcedure, "CMS_Game_Search", parameters).Tables[0];
        }

        //Update Trạng thái 
        public virtual void UpdateStatus(int idGame, int status, int param)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdGame", idGame),
                new SqlParameter("@Status", status),
                new SqlParameter("@Param", param),
			};

            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "CMS_Game_UpdateStatus", parameters);
        }


		#endregion
        public DataTable GetTop()
        {
            try
            {
                string sql = " SELECT top 20 * FROM CMS_Game Order by CreatedOn desc";
                return SqlHelper.ExecuteDataset(ConnectionString, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }
        }
	}
}
